package mogo.test;

import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class jsonfaetter extends Activity {
    
	private class Net
	{
		public String getNet() {
			return net;
		}
		public void setNet(String net) {
			this.net = net;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		String net;
		String id;
	}
	
	
	java.util.HashSet<Net> data = new java.util.HashSet<Net>();  
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv = (TextView) findViewById( R.id.TextView01 ); 
        
        for ( int i = 0; i < 10000; i++ )
        {
        	Net n = new Net();
        	n.setId("" + i );
        	n.setNet("" + i );
        	data.add( n );
        }
        try
        {
        	Date d = new Date();
        	long start  = d.getTime();
        	// JSON lib metoden.
        	org.json.JSONArray jsondata = new JSONArray();
        
        	for ( Net n : data ) 
        	{
        		JSONObject o = new JSONObject();	
				o.put( "Net" , n.getNet() );
				o.put( "Id" , n.getId() );
				jsondata.put(o);
        	}
        	
        	Log.w( "perf. test",  jsondata.toString() );
        	d = new Date();
        	
        	tv.setText( "ms consumed(JSON Lib): " + ( d.getTime() - start )  );

        	
        	d = new Date();
        	start = d.getTime();
        	
        	StringBuilder sb = new StringBuilder();
        	
	    	sb.append( "[" );
	    	boolean first = true;
	    	
	    	for ( Net l : data )
	    	{
	    		if ( ! first )
	    		{
	    			sb.append(",");
	    		}
	    		else
	    		{
	    			first = false;
	    		}
	    		sb.append("{\"Net\":\"");
	    		sb.append( l.getId() );
	    		sb.append( "\",\"Id\":\"" );
	    		sb.append( l.getNet() );
	    		sb.append( "\"}" );
	    		
	    	}
	    	sb.append( "]" );
	    	
	    	Log.w( "perf. test",  sb.toString() );
	    	d = new Date();
	    	
        	tv.setText( tv.getText() + "\nms consumed(sb): " + ( d.getTime() - start ) );
        	
		} 
        catch (JSONException e)
        {			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}